package com.xiu.travel.controller;

import com.xiu.travel.util.api.CommonResult;
import com.xiu.travel.domain.ResPathColorDO;
import com.xiu.travel.security.FileService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文件上传控制器
 * </p>
 *
 * @author qianl
 * @since 2023-2-6
 */
@RestController
@RequestMapping("/admin/open/v1/file")
@CrossOrigin
@Tag(name = "文件上传接口")
public class FileController
{
    @Autowired
    private FileService fileService;

    @PostMapping("fileUpload")
    @Tag(name = "文件上传接口")
    public CommonResult<ResPathColorDO> upFile(@RequestParam("file") @Parameter(description = "上传文件") MultipartFile file) throws Exception
    {
        return fileService.upFile(file);
    }

    @PostMapping("picUpload")
    @Tag(name = "图片上传接口")
    public CommonResult<ResPathColorDO> picUpload(@RequestParam("pic") @Parameter(description = "上传文件") MultipartFile file,@RequestParam("picName") String picName) throws Exception
    {
        return fileService.picUpload(file,picName);
    }

    @PostMapping("fileUploadList")
    @Tag(name = "多文件上传接口")
    public CommonResult<List<ResPathColorDO>> upFiles(@Parameter(description = "多个上传文件用流输入") HttpServletRequest request) throws Exception
    {
        return fileService.upFiles(request);
    }

    @SneakyThrows
    @GetMapping("download/{name}")
    @Tag(name = "文件下载")
    public void downloadFile(@PathVariable @Parameter(description = "文件名") String name, HttpServletResponse response)
    {
        fileService.downloadFile(name, response);
    }

}
